package com.flying.invest.assets.service;

import java.util.Date;

import com.flying.common.annotation.DaoCreate;
import com.flying.common.annotation.DaoQuery;
import com.flying.common.annotation.DaoRemove;
import com.flying.common.annotation.DaoUpdate;
import com.flying.common.util.Codes;
import com.flying.framework.annotation.MethodInfo;
import com.flying.framework.annotation.Param;
import com.flying.framework.annotation.Service;
import com.flying.framework.context.ServiceContext;
import com.flying.framework.exception.AppException;
import com.flying.framework.exception.ObjectNotFoundException;
import com.flying.framework.model.ActionResult;
import com.flying.framework.model.ModelFactory;
import com.flying.framework.model.QueryResult;
import com.flying.framework.security.Principal;
import com.flying.framework.service.AbstractService;
import com.flying.invest.assets.model.Assets;
import com.flying.invest.assets.model.AssetsFavorite;
import com.flying.invest.assets.model.AssetsFavorite.Status;
import com.flying.invest.invest.model.InvestmentTransaction;

@Service(value="AssetsFavoriteService", desc="投资品种收藏")
public class AssetsFavoriteService extends AbstractService{
    private AssetsService assetsService;
    public void setAssetsService(AssetsService s) {
    	this.assetsService = s;
    }
	@MethodInfo("新增")
	@DaoCreate(entity=AssetsFavorite.ENTITY_NAME)
	public AssetsFavorite create(
			@Param(value=AssetsFavorite.ASSETS_ID, required=true,	desc="产品ID") Long assets_id,
			@Param(value=AssetsFavorite.USER_ID, required=false,	desc="用户ID") Long user_id,
			@Param(value=AssetsFavorite.NAME, required=false,	desc="名称") String name,
			@Param(value=AssetsFavorite.REMARKS, required=false,	desc="说明") String remarks,
			@Param(value=AssetsFavorite.STATUS, required=false,	desc="状态", enumClass=Status.class) Integer status,
			@Param(value=AssetsFavorite.TERM_TYPE, required=false,	desc="投资周期") Integer term_type,
			@Param(value=AssetsFavorite.CREATOR, required=false,	desc="创建人") String creator,
			@Param(value=AssetsFavorite.CREATE_TIME, required=false,	desc="创建时间") Date create_time
		) {
		Principal p = ServiceContext.getContext().getPrincipal();
		if(user_id == null) user_id = Long.parseLong(p.getId());
		Assets assets = this.assetsService.findById(assets_id);
		if(name == null) name = assets.getName();
		if(remarks == null) remarks = assets.getRemarks();
		if(status == null) status = Status.VALID.value();
		try {
			this.findById(assets_id, user_id);
			throw new AppException("0101", "已经收藏" + name);
		} catch (ObjectNotFoundException e) {}
		return ModelFactory.create(AssetsFavorite.class, 
				AssetsFavorite.USER_ID, user_id,
				AssetsFavorite.NAME, name,
				AssetsFavorite.REMARKS, remarks,
				AssetsFavorite.STATUS, status,
				AssetsFavorite.CREATOR, p.getId(),
				AssetsFavorite.CREATE_TIME, new Date());
	}

	@MethodInfo("修改")
	@DaoUpdate(entity=AssetsFavorite.ENTITY_NAME)
	public ActionResult update(
			@Param(value=AssetsFavorite.ASSETS_ID, required=true,	desc="产品ID", tag="=") Long assets_id,
			@Param(value=AssetsFavorite.USER_ID, required=false,	desc="用户ID", tag="=") Long user_id,
			@Param(value=AssetsFavorite.NAME, required=true,	desc="名称") String name,
			@Param(value=AssetsFavorite.REMARKS, required=false,	desc="说明") String remarks,
			@Param(value=AssetsFavorite.STATUS, required=false,	desc="状态", enumClass=Status.class) Integer status,
			@Param(value=AssetsFavorite.TERM_TYPE, required=false,	desc="投资周期") Integer term_type,
			@Param(value=AssetsFavorite.UPDATOR, required=false,	desc="修改人") String updator,
			@Param(value=AssetsFavorite.UPDATE_TIME, required=false,	desc="更新时间") Date update_time
		) {
		Principal p = ServiceContext.getContext().getPrincipal();
		if(user_id == null) user_id = Long.parseLong(p.getId());
		return ModelFactory.create(ActionResult.class, 
				AssetsFavorite.USER_ID, user_id,
				AssetsFavorite.UPDATOR, p.getId(),
				AssetsFavorite.UPDATE_TIME, new Date());
	}

	@MethodInfo("删除")
	@DaoRemove(entity=AssetsFavorite.ENTITY_NAME)
	public ActionResult remove(
			@Param(value=AssetsFavorite.ASSETS_ID, required=true,	desc="产品ID", tag="=") Long assets_id,
			@Param(value=AssetsFavorite.USER_ID, required=false,	desc="用户ID", tag="=") Long user_id
		) {
		Principal p = ServiceContext.getContext().getPrincipal();
		if(user_id == null) user_id = Long.parseLong(p.getId());
		return ModelFactory.create(ActionResult.class, 
				AssetsFavorite.USER_ID, user_id);
	}

	@MethodInfo("搜索")
	@DaoQuery(entity=AssetsFavorite.ENTITY_NAME, pageable=true, modelClass=AssetsFavorite.class)
	public QueryResult<AssetsFavorite> findAll(
			@Param(value=Codes.PAGE_NUM, 	required=false, desc="分页页号，默认0") int page,
			@Param(value=Codes.PAGE_SIZE, 	required=false, desc="分页大小，默认10") int rows) {
		return null;
	}

	@MethodInfo("查询")
	@DaoQuery(entity=AssetsFavorite.ENTITY_NAME, single=true, throwsNotFoundException=true )
	public AssetsFavorite findById(
			@Param(value=AssetsFavorite.ASSETS_ID, required=true,	desc="产品ID") Long assets_id,
			@Param(value=AssetsFavorite.USER_ID, required=true,	desc="用户ID") Long user_id
		) {
		return null;
	}

}